package util.thread.ordertoperform;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Semaphore;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2020/12/2
 * @Version：1.0.0
 */
public class SemaphoreTest {

    private static final Logger logger = LoggerFactory.getLogger(SemaphoreTest.class);

    private static final Semaphore semaphore1 = new Semaphore(0);
    private static final Semaphore semaphore2 = new Semaphore(0);

    class One extends Thread {
        @Override
        public void run() {
            logger.info("=====》One线程执行完成...");
            semaphore1.release();
        }
    }

    class Two extends Thread {
        @Override
        public void run() {
            try {
                semaphore1.acquire();
                logger.info("=====》Two线程执行完成...");
                semaphore2.release();
            } catch (InterruptedException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    class Three extends Thread {
        @Override
        public void run() {
            try {
                semaphore2.acquire();
                logger.info("======》Three线程执行完成...");
                semaphore2.release();
            } catch (InterruptedException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    @Test
    public void test1() throws InterruptedException {
        Thread one = new One();
        one.start();
        Thread two = new Two();
        two.start();
        Thread three = new Three();
        three.start();
        Thread.sleep(5000);
        logger.info("=====>三个子线程结束...");
    }
}
